import {
    month, year, day, days
} from "../../../utils/util";
import { activityMonth } from "../../../api/activity"
import { getStorage } from "../../../utils/localStorage"
Page({
    data: {
        navData: {
            level: 2,
            title: "日期选择",
        },
        user: getStorage("user"),
        currentMonth: null,
        todayIdx: 0, //判断每个月一号是星期几
        activityList: [], //活动列表
        titleData: {}, //title字段
        xzIndex: null,
    },
    onLoad(options) {
        this.setData({
            currentMonth:year+"-"+month,
        });
        this.getActivityMonth(this.data.currentMonth + "-" + "01");
    },
    onShow() {

    },
    onReady() {

    },
    //获取当前月份所有的活动日期
    getActivityMonth(date) {
        let currentDate = new Date(date.replace(/-/g, '/')),
            currentYear = currentDate.getFullYear(),
            currentMonth = (currentDate.getMonth() + 1) < 10 ? "0" + (currentDate.getMonth() + 1) : (currentDate.getMonth() + 1);
        activityMonth({ userId: this.data.user.id, date: date }).then(res => {
            res.data.data.forEach(item => {
                if (item.imageScale) {
                    item.imageScale = item.imageScale.split(",");
                };
            });
            this.setData({
                todayIdx: new Date(currentYear + '/' + currentMonth + "/" + 1).getDay(),
                activityList: res.data.data,
                titleData: res.data.data[0],
            });
        });
    },
    // 关闭事件
    back() {
        wx.navigateBack({
            delta: 1,
        });
    },
    //日历左切换
    switchLeft(e) {
        this.setData({
            xzIndex: null
        });
        let item = e.currentTarget.dataset.item.replace(/-/g, "/"),
            date = new Date(item),
            year = date.getFullYear(),
            month = date.getMonth() + 1,
            index = month - 1;
        if (index < 1) {
            this.setData({
                currentMonth: (year - 1) + '-' + 12
            });
        } else {
            this.setData({
                currentMonth: year + '-' + ((month - 1) < 10 ? "0" + (month - 1) : (month - 1))
            });
        };
        this.getActivityMonth(this.data.currentMonth + '-01');

    },
    //日历右切换
    switchRight(e) {
        this.setData({
            xzIndex: null
        });
        let item = e.currentTarget.dataset.item.replace(/-/g, "/"),
            date = new Date(item),
            year = date.getFullYear(),
            month = date.getMonth() + 1,
            index = month + 1;
        if (index > 12) {
            this.setData({
                currentMonth: (year + 1) + '-' + '01'
            });
        } else {
            this.setData({
                currentMonth: year + '-' + ((month + 1) < 10 ? "0" + (month + 1) : (month + 1))
            });
        };
        this.getActivityMonth(this.data.currentMonth + '-01');
    },
    //日期点击事件
    dayClick(e) {
        let item = e.currentTarget.dataset.item,
            index = e.currentTarget.dataset.index;
        if (item.numberDayFlag) {
            // item.id = item.userId;
            this.setData({
                titleData: item,
                xzIndex: index
            });
        } else {
            wx.showToast({
                icon: 'none',
                title: '当前日期没有活动',
            })
        };
    },
    //确定按钮事件
    submit(e) {
        let item = e.currentTarget.dataset.item;
        if (item.numberDayFlag) {
            wx.navigateTo({
                url: '/pagesShouYe/pages/bnnerDetailMore/index?id=' + item.id,
            });
        } else {
            wx.showToast({
                icon: 'none',
                title: '当前日期没有活动',
            })
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})